package com.duniptech.engine.core.modeling;

import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import com.duniptech.engine.core.modeling.api.IMessage;

public class Message implements IMessage {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Map<String,Collection<Object>> messages;
	
	public Message() {
		messages = new HashMap<String,Collection<Object>>();
	}
	
	public void add(String portName, Object value) {
		if(value==null) messages.put(portName, null);
		else {
			Collection<Object> values = messages.get(portName);
			if(values==null) {
				values = new HashSet<Object>();
				messages.put(portName, values);
			}
			values.add(value);
		}
	}
	
	public Collection<Object> getValuesOnPort(String portName) {
		Collection<Object> values = messages.get(portName);
		if(values==null) return new HashSet<Object>();
		return values;
	}
	
	public boolean isEmpty() {
		return messages.isEmpty();
	}
	
	public void receive(String portFrom, IMessage newMessage, String portTo) {
		Collection<Object> valuesFrom = newMessage.getValuesOnPort(portFrom);
		messages.put(portTo, valuesFrom);
	}
	
	public void print(){
		StringBuffer sb = new StringBuffer();
		sb.append("Message->\n");
		for(String port: messages.keySet()){
			Collection<Object> values = getValuesOnPort(port);
			sb.append("-[Port: "+port+"; Values:{ ");
			for(Object o: values){
				sb.append(o+" ");
			}
			sb.append("}]\n");
		}
		
		System.out.println(sb.toString());
		
	}
}
